﻿
using Google.Protobuf;
using Google.Protobuf.WellKnownTypes;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Sockets;

namespace ss
{
    class Program
    {
        public class AsyncUserToken
        {
            public Socket Socket;
            public int bodyLength;
            List<byte> arrayList = new List<byte>();
            public byte[] message;
            public int recLength = 0;
            public int m_numConnectedSockets;      // the total number of clients connected to the server连接到服务器的客户端的唯一ID
            public LengthDecode LD;//定义分包处理的委托
            public void OnRecive(byte[] result)
            {
                arrayList.AddRange(result);
                OnRead();

            }
            public void OnRead() {
                while (true)
                {
                    byte[] buffer = LD(ref arrayList);
                    if (buffer == null) return;
                    Person person = Person.Parser.ParseFrom(buffer);
                    Console.WriteLine("OnRead：" + System.Threading.Thread.CurrentThread.ManagedThreadId.ToString() + "   " + person.BodyLength);
                }
                //OnRead();
            }

        }
        static Program Instance = new Program();
        Server server;
        private const int port = 11000;
        static void Main(string[] args)
        {
            Console.WriteLine("当前线程ID：" + System.Threading.Thread.CurrentThread.ManagedThreadId.ToString());

            Instance.server = new Server(100000, 1024);
            IPHostEntry ipHostInfo = Dns.GetHostEntry("127.0.0.1");
            IPAddress ipAddress = ipHostInfo.AddressList[1];
            IPEndPoint remoteEP = new IPEndPoint(ipAddress, port);
            Instance.server.Init();
            Instance.server.Start(remoteEP);


            //SocketManager m_socket = new SocketManager(200, 1024);
            //m_socket.Init();
            //m_socket.Start(new IPEndPoint(IPAddress.Any, 13909));

        }
    }
}
